* Set directories


cd /Users/byrned/Dropbox/Research/InformedSources/Replication/Analysis/
local datdir /Users/byrned/Dropbox/Research/InformedSources/Replication/Build/Output/
est clear

*------------------- Table 1 Summary Statistics (Melbourne) -------------------*

* Prices
use `datdir'is_mel_p_2005_2019, clear
keep if t>=td(01may2015) & t<=td(31dec2017)4
keep if !missing(p)
gen pos=cond(t>=td(15apr2016),1,0)
gen pre=1-pos

eststo pre: quietly estpost summarize ///
    p c marg if pos == 0
eststo pos: quietly estpost summarize ///
    p c marg if pos == 1
eststo diff: estpost ttest ///
    p c marg, by(pre) unequal

esttab pre pos using "Output/tab1a_sumstats.tex", replace ///
	cells("mean(pattern(1 1 0) fmt(2)) sd(pattern(1 1 0)) b(star pattern(0 0 1) fmt(2)) se(pattern(0 0 1) par fmt(3))") ///
	varlabels(p "Price" c "Terminal Gate Price" marg "Margin") nonumber	///
	nomtitle nonote noobs label booktabs f posthead("") collabels(none) ///
	starl($^{*}$ .1 $^{**}$ .05 $^{***}$ .01)

* Dates
use `datdir'is_mel_p_2005_2019, clear
keep if t>=td(01may2015) & t<=td(31dec2017)
keep if !missing(p)
sort t id
by t: keep if _n==1
gen dates=1
gen pos=cond(t>=td(15apr2016),1,0)
sort pos
collapse (sum) dates, by(pos)
drop pos 
xpose, clear
tostring v1 v2, force replace
replace v1="\multicolumn{2}{c}{" +v1+"}"
replace v2="\multicolumn{2}{c}{" +v2+"}"
gen c1="Dates &"
gen c2="&"
order c1 v1 c2 v2
outsheet using Output/tab1b_sumstats.tex, delim(" ") nonames noquote replace


* Panel dimensions (pre)
use `datdir'is_mel_p_2005_2019, clear
keep if t>=td(01may2015) & t<=td(31dec2017)
keep if !missing(p)
drop if bid==.
sort id t bid
by id: gen tag = _n == 1
keep if tag == 1
collapse (count) id, by(bid)
rename id stns
set obs 7
egen totstns=total(stns)
gen pct=100*(stns/totstns)
qui replace pct=round(pct,1)
gen totpct=100
qui replace stns=totstns if _n==_N
qui replace pct=totpct if _n==_N
decode bid, gen(brand)
qui replace brand="Total" if _n==_N
tostring stns, force replace
tostring pct, force replace
gen str_pre="~~"+brand+"& \multicolumn{2}{r}{"+stns+"~~~("+pct+"\%)}"
qui replace str_pre="~~"+brand+"& \multicolumn{2}{r}{"+stns+"~~~~~("+pct+"\%)}" if length(pct)==1
qui replace str_pre="~~"+brand+"& \multicolumn{2}{r}{~~~"+stns+"~~~("+pct+"\%)}" if _n==_N
keep brand str_pre
gen row=_n
sort row 
save Temp/temp, replace 

* Panel dimensions (post)
use `datdir'is_mel_p_2005_2019, clear
keep if t>=td(15apr2016) & t<=td(31dec2017)
keep if !missing(p)
drop if bid==.
sort id t bid
by id: gen tag = _n == 1
keep if tag == 1
collapse (count) id, by(bid)
rename id stns
set obs 7
egen totstns=total(stns)
gen pct=100*(stns/totstns)
qui replace pct=round(pct,1)
gen totpct=100
qui replace stns=totstns if _n==_N
qui replace pct=totpct if _n==_N
decode bid, gen(brand)
qui replace brand="Total" if _n==_N
tostring stns, force replace
tostring pct, force replace
gen str_pos="~~"+"& \multicolumn{2}{r}{"+stns+"~~~("+pct+"\%)} \\"
qui replace str_pos="~~"+"& \multicolumn{2}{r}{"+stns+"~~~~~("+pct+"\%)} \\" if length(pct)==1
qui replace str_pos="~~"+"& \multicolumn{2}{r}{~~~"+stns+"~~~("+pct+"\%)}" if _n==_N
keep brand str_pos
gen row=_n
sort row 
merge 1:1 row using Temp/temp, nogen
keep str_pre str_pos
order str_pre str_pos
qui replace str_pre=subinstr(str_pre,"(100\%)","~~~~",1) 
qui replace str_pos=subinstr(str_pos,"(100\%)","~~~~",1) 
outsheet using Output/tab1c_sumstats.tex, delim(" ") nonames noquote replace

* Observations (pre)
use `datdir'is_mel_p_2005_2019, clear
keep if t>=td(01may2015) & t<td(15apr2016)
keep if !missing(p)
gen obs=1
collapse(sum) obs manual
gen diff=obs-manual
set obs 3
qui replace obs=obs[_n-2] if _n==3
qui replace obs=manual[_n-1] if _n==2
qui replace obs=diff if _n==1
egen totobs=max(obs)
gen pct=round(100*(obs/totobs),1)
tostring obs, replace
tostring pct, replace
gen name="~~ Electronically collected & \multicolumn{2}{r}{" if _n==1
replace name="~~ Manually collected & \multicolumn{2}{r}{" if _n==2
replace name="~~ Total & \multicolumn{2}{r}{" if _n==3
gen str_pre=name+obs+"~~~("+pct+"\%)} "
qui replace str_pre=name+obs+"~~~("+pct+"\%)}" if _n==_N
keep str_pre
gen row=_n
sort row
save Temp/temp, replace

* Observations (post)
use `datdir'is_mel_p_2005_2019, clear
keep if t>=td(15apr2016) & t<=td(31dec2017)
keep if !missing(p)
gen obs=1
collapse(sum) obs manual
gen diff=obs-manual
set obs 3
qui replace obs=obs[_n-2] if _n==3
qui replace obs=manual[_n-1] if _n==2
qui replace obs=diff if _n==1
egen totobs=max(obs)
gen pct=round(100*(obs/totobs),1)
tostring obs, replace
tostring pct, replace
gen name="~~ & \multicolumn{2}{r}{" if _n==1
replace name="~~ & \multicolumn{2}{r}{" if _n==2
replace name="~~ & \multicolumn{2}{r}{" if _n==3
gen str_pos=name+obs+"~~~("+pct+"\%)} \\"
qui replace str_pos=name+obs+"~~~("+pct+"\%)}" if _n==_N
keep str_pos
gen row=_n
sort row
merge 1:1 row using Temp/temp, nogen
keep str_pre str_pos
order str_pre str_pos
qui replace str_pre=subinstr(str_pre,"(100\%)","~~~~",1) 
qui replace str_pos=subinstr(str_pos,"(100\%)","~~~~",1) 
outsheet using Output/tab1d_sumstats.tex, delim(" ") nonames noquote replace
